package com.fingard.dsp.bank.directbank.rccosd01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @author Parker on 2019/7/17.
 * @version 1.0
 */
public class RCCOSD01Base extends DirectBase {
    protected static TransIDHelper idHelper = new TransIDHelper(1,9999);

    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    protected void writeCommonHeader(XmlTextWriter xmlWriter, RCCOSD01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.bsnCode)) {
            throw new Exception("未赋值bsnCode");
        }
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("Message");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("serialNo", p_bankHeader.serialNo);
        xmlWriter.writeElementString("reqTime", Format.DateTime14Format.format(new Date()));
        xmlWriter.writeElementString("bsnCode", p_bankHeader.bsnCode);
        xmlWriter.writeElementString("CifNo", bankFront.org);
        xmlWriter.writeEndElement();
    }

    /**
     * 发送银行前置
     *
     * @param p_reqStr
     * @return
     * @throws Exception
     */
    protected String[] sendToBank(String p_reqStr, RCCOSD01Header bankHead) throws Exception {
        String[] retStr = new String[]{"", ""};
        String Sigdata = "";
        if ("QRCB003".equals(bankHead.bsnCode) || "QRCB004".equals(bankHead.bsnCode)) {
            Sigdata = "1";
        } else {
            Sigdata = "0";
        }
        String tmpUrl = "http://" + bankFront.serverIP + ":" + bankFront.serverPort + "/eweb/b2econnect.do?userPassword=" + bankFront.loginPasswd + "&Sigdata=" + Sigdata;
        WriteBankLogLn("1.提交银行的地址：" + tmpUrl);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(tmpUrl);
        tmpWebRequest.setConnectTimeout(100000);
        tmpWebRequest.setRequestProperty("Method", "POST");
        tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        tmpWebRequest.setRequestProperty("connection", "close");//设置连接状态
        byte[] tmpUpBytes = p_reqStr.getBytes(getCharset());
        retStr = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3(retStr[1]);
        return retStr;
    }

    /**
     * 文件上传
     * @param p_FileName
     * @param p_TransCode
     * @param p_SerialNo
     * @return
     */
    protected String[] uploadFileToBank(String p_FileName, String p_TransCode, String p_SerialNo, String code, String payAct) {
        String[] retStr = new String[]{"", ""};
        try {
            String tmpUrl = "";
            if ("198803".equals(code)){
                String subCifNo = getBankConfig().getMapValueByKey("SubCifNoMapping", payAct);// 核心客户号
                tmpUrl = "http://" + bankFront.serverIP + ":" + bankFront.serverPort + "/eweb/uploadFile.do?userPassword=" + bankFront.loginPasswd + "&SIGDATA=1&fileName=" + p_FileName + "&delete=0&transCode=" + p_TransCode + "&CifNo=" + bankFront.org + "&SubCifNo=" + subCifNo + "&serialNo=" +p_SerialNo+ "&reqTime=" + Format.DateTime14Format.format(new Date());
            }else{
                tmpUrl = "http://" + bankFront.serverIP + ":" + bankFront.serverPort + "/eweb/uploadFile.do?userPassword=" + bankFront.loginPasswd + "&SIGDATA=1&fileName=" + p_FileName + "&delete=0&transCode=" + p_TransCode + "&CifNo=" + bankFront.org + "&serialNo=" + p_SerialNo + "&reqTime=" + Format.DateTime14Format.format(new Date());
            }
            WriteBankLogLn("1.提交银行的地址：" + tmpUrl);
            WebRequest tmpWebRequest = new WebRequest(tmpUrl);
            tmpWebRequest.setConnectTimeout(100000);
            tmpWebRequest.setRequestProperty("Method", "POST");
            tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            tmpWebRequest.setRequestProperty("connection", "close");//设置连接状态
            retStr = tmpWebRequest.upload("".getBytes(getCharset()), "UTF-8");//银行不需要发送数据
            WriteBankLogStep3(retStr[1]);
        } catch (Exception e) {
            WriteBankLogLn("与银行通讯失败,上传文件失败：" + e.getMessage());
        }
        return retStr;
    }

    /**
     * 文件下载
     * @param p_FileName
     * @param p_TransCode
     * @param p_SerialNo
     * @param p_OriSerialNo
     * @return
     */
    protected String[] downloadFileFromBank(String p_FileName, String p_TransCode, String p_SerialNo, String p_OriSerialNo) {
        String[] retStr = new String[]{"", ""};
        try {
            String tmpUrl = "http://" + bankFront.serverIP + ":" + bankFront.serverPort + "/eweb/downloadFile.do?userPassword=" + bankFront.loginPasswd + "&SIGDATA=1&fileName=" + p_FileName + "&transCode=" + p_TransCode + "&CifNo=" + bankFront.org + "&serialNo=" + p_SerialNo + "&reqTime=" + Format.DateTime14Format.format(new Date())+"&OriSerialNo="+p_OriSerialNo;
            WriteBankLogLn("1.提交银行的地址：" + tmpUrl);
            WebRequest tmpWebRequest = new WebRequest(tmpUrl);
            tmpWebRequest.setConnectTimeout(100000);
            tmpWebRequest.setRequestProperty("Method", "POST");
            tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            tmpWebRequest.setRequestProperty("connection", "close");//设置连接状态
            retStr = tmpWebRequest.upload("".getBytes(getCharset()), "UTF-8");//银行不需要发送数据
            WriteBankLogStep3(retStr[1]);
        } catch (Exception e) {
            WriteBankLogLn("与银行通讯失败,上传文件失败：" + e.getMessage());
        }
        return retStr;
    }

    protected String getQryTransID(){
        return Format.DateTime12Year2Format.format(new Date())+idHelper.getNextIDStr();
    }
}
